/**
 * 
 */
package info.niwota.dc;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.Window;
import android.view.WindowManager;

/**
 * @author qiangli
 *
 */
public class RestrictedActivity extends ClockActivity {
	public static final String ACTION_POWERUP_SUSPEND = "info.niwota.intent.ACTION_POWERUP_SUSPEND";
	
	public static final String ACTION_POWERUP_QUIT = "info.niwota.intent.ACTION_POWERUP_QUIT";
	
	private static final String TAG = "RestrictedActivity";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		if (Debug.DEBUG) {
			Log.d(TAG, "onCreate " + savedInstanceState);
		}
		
		setSecureFullscreen();
		
		super.onCreate(savedInstanceState);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return false;
	}

	@Override
	protected void onPause() {
		if (Debug.DEBUG) {
			Log.d(TAG, "onPause");
		}
		super.onPause();
	}

	@Override
	protected void onResume() {
		if (Debug.DEBUG) {
			Log.d(TAG, "onResume");
		}
		super.onResume();
		
		broadcast(ACTION_POWERUP_SUSPEND);
	}

	@Override
	protected void onDestroy() {
		if (Debug.DEBUG) {
			Log.d(TAG, "onDestroy");
		}
		super.onDestroy();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (Debug.DEBUG) {
			Log.d(TAG, "onKeyDown key: " + keyCode);
		}
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			quit();
			return true;
		}
		
		return true;
	}

	protected void quit() {
		finish();
		
		broadcast(ACTION_POWERUP_QUIT);
	}

	private void broadcast(String action) {
		Intent i = new Intent(action);
		sendOrderedBroadcast(i, null);
	}

	@Override
	protected void doHelp() {
	}

	@Override
	protected void doPickDateTime() {
	}

	@Override
	protected void doPickFace() {
	}

	@Override
	protected void doQuit() {
		quit();
	}

	@Override
	protected void doSetBackground() {
	}

	@Override
	protected void doSettings() {
	}
	
	private void setSecureFullscreen() {
		final Window w = getWindow();
		w.requestFeature(Window.FEATURE_NO_TITLE);
		w.clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
		w.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
		w.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
	}

}
